package cn.jji8.KnapsackToGo.diaoduqi.io;

import cn.jji8.KnapsackToGo.main;
import org.bukkit.Bukkit;
import org.bukkit.advancement.Advancement;
import org.bukkit.advancement.AdvancementProgress;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 同步玩家成就
 * */
public class ChengJiu implements Io {
    @Override
    public void duqu(Player wanjia){
        long startTime = System.currentTimeMillis();
        File File = new File(main.peizi.工作路径 + "/成就/" + wanjia.getName()+".yml");
        YamlConfiguration wanjiawenjian = YamlConfiguration.loadConfiguration(File);
        Iterator<Advancement> advancementList = Bukkit.advancementIterator();
        while (advancementList.hasNext()) {
            Advancement advancement = advancementList.next();
            String advancementName = advancement.getKey().toString();//成就的名字

            AdvancementProgress advancementProgress = wanjia.getAdvancementProgress(advancement);
            List<String> complete = wanjiawenjian.getStringList(advancementName+".达成的条件");
            List<String> noComplete = wanjiawenjian.getStringList(advancementName+".未达到的条件");
            BukkitRunnable BukkitRunnable = new BukkitRunnable() {
                @Override
                public void run() {
                    for(String a:complete){
                        advancementProgress.awardCriteria(a);
                    }
                    for(String a:noComplete){
                        advancementProgress.revokeCriteria(a);
                    }
                }
            };
            BukkitRunnable.runTask(main.main);
        }
        long endTime = System.currentTimeMillis();
        if(main.peizi.后台显示更多信息) Bukkit.getLogger().info("[跨服背包同步]:"+wanjia.getName()+"成就加载用时间： "+(endTime-startTime)+"ns");
    }

    @Override
    public void xieru(Player wanjia){
        long startTime = System.currentTimeMillis();
        File File = new File(main.peizi.工作路径 + "/成就/" + wanjia.getName()+".yml");
        YamlConfiguration wanjiawenjian = YamlConfiguration.loadConfiguration(File);
        Iterator<Advancement> advancementList = Bukkit.advancementIterator();
        while (advancementList.hasNext()) {
            Advancement advancement = advancementList.next();
            String advancementName = advancement.getKey().toString();//成就的名字

            AdvancementProgress advancementProgress = wanjia.getAdvancementProgress(advancement);
            Collection<String> completeColl = advancementProgress.getAwardedCriteria();//已到达的全部条件
            ArrayList<String> completeList = new ArrayList<>();
            for(String complete:completeColl){
                completeList.add(complete);
            }
            Collection<String> criteriaColl = advancementProgress.getRemainingCriteria();//未达到的全部条件
            ArrayList<String> criteriaList = new ArrayList<>();
            for(String criteria:criteriaColl){
                criteriaList.add(criteria);
            }
            wanjiawenjian.set(advancementName+".达成的条件",completeList);
            wanjiawenjian.set(advancementName+".未达到的条件",criteriaList);
        }
        try {
            wanjiawenjian.save(File);
        } catch (IOException e) {
            e.printStackTrace();
            Bukkit.getLogger().warning("[跨服背包同步]:成就保存失败,玩家成就未保存，请检查写入权限，或磁盘空间");
        }
        long endTime = System.currentTimeMillis();
        if(main.peizi.后台显示更多信息) Bukkit.getLogger().info("[跨服背包同步]:"+wanjia.getName()+"成就保存用时间： "+(endTime-startTime)+"ns");
    }
}
